<template lang="pug">
  .page.h-100
    .clearfix
      .search-form.mb-10.pl-10.pr-10
        Form.ivu-row.ivu-form-basic(ref="formData" :model="formData" :rules="rules" :label-width="90")
          FormItem.form-item.ivu-col.pr-20.mb-15(v-for="(item, index) of formItem" :label="item.label" :key="index" :class="item.style" :prop="item.prop")
            template(v-if="item.type === 'select'")
              Select(v-model="formData[item.model]" :placeholder="item.placeholder" clearable filterable)
                Option(
                  v-for="(c_item, index) in arrListDown[item.options]"
                  :value="c_item.label"
                  :key="index"
                ) {{ c_item.label }}
            template(v-if="item.type === 'input'")
              Input(v-model="formData[item.model]" :placeholder="item.placeholder" clearable)
            template(v-if="item.type === 'button'")
              Button(type="info" @click="searchFn") 查询

      .pl-20.pr-20.bg-white
        Table.hasTitle-table.beautiful-table(:columns="thead" :data="tableData" :stripe="true" ref="demoTable" @on-selection-change="handleOnSelectChange")
          .clearfix.font-16(slot="header")
            .table-title-lefttxt.pull-left
              |合计
              span.text-info.pl-5.pr-5.font-weight {{tableComone.totalRows}}
              |条
            .pull-right.table-title-btns
              Button.mr-10(v-if="hasPostFinanceProduct" type="info" @click="handleShowCreateModal(null)") 新增
          .clearfix.pl-20.pr-20(slot="footer")
            Page(
              :total="tableComone.totalRows"
              @on-change="onChangePage"
              @on-page-size-change="onpageSizeChange"
              show-sizer
              show-total
              placement="top"
              prev-text="上一页"
              next-text="下一页"
              :page-size-opts="[10, 20, 30, 50, 100]"
            )
          template(slot-scope="{ row, index }" slot="action")
            Button.mr-10(v-if="hasPostFinanceProduct" type="text" size="small" @click="handleShowCreateModal(row.id)") 编辑
            Button.mr-10(v-if="hasDelete" type="text" size="small" @click="isDeleteModal(row.id)") 删除
            Button(v-if="hasEnableFinanceProduct && row.status == 0" type="text" size="small" @click="enabledList(row.id, 1)") 启用
            Button(v-if="hasEnableFinanceProduct && row.status == 1" type="text" size="small" @click="enabledList(row.id, 0)") 禁用

    //- 新建弹窗
    CreateModal(ref="CreateModal" @query="searchFn")
</template>

<script>
import { Common } from '@/utils/common.js'
import { FinanceApi } from '@/api/Finance.api.js'
import { FinanceProductApi } from '@/api/FinanceProduct.api.js'
import { PageListThead } from './tableHead.js'
import _cloneDeep from 'clone-deep'
import CreateModal from './modal/CreateModal.vue'

export default {
  name: 'financeProduct', // 产品清单页面

  components: {
    CreateModal
  },

  computed: {
    actionArr () {
      let arr = this.$store.state.user.actionAccess.map(item => {
        return item.code
      })
      return arr
    },
    // 查询
    hasGetFinanceProductList () {
      return this.actionArr.indexOf('GetFinanceProductList') > -1
    },
    // 删除
    hasDelete () {
      return this.actionArr.indexOf('Delete') > -1
    },
    // 新增编辑
    hasPostFinanceProduct () {
      return this.actionArr.indexOf('PostFinanceProduct') > -1
    },
    // 启用禁用
    hasEnableFinanceProduct () {
      return this.actionArr.indexOf('EnableFinanceProduct') > -1
    }
  },

  data () {
    return {
      thead: _cloneDeep(PageListThead),
      tableData: [],
      selectedId: [], // 勾选的id集合,array格式
      selectedArr: [], // 勾选的id, name数组集合
      tableComone: {
        pageIndex: 1,
        totalRows: 0,
        pageSize: 10
      },
      formData: {
        businessName: '',
        productType: ''
      },
      formItem: [
        { type: 'select', label: '业务板块：', placeholder: '请输入业务板块', prop: 'businessName', model: 'businessName', style: 'ivu-col-span-6', options: 'FinanceBusinessSegments' },
        { type: 'select', label: '产品类别：', placeholder: '请输入产品类别', prop: 'productType', model: 'productType', style: 'ivu-col-span-6', options: 'FinanceProductType' },
        { type: 'button', style: 'ivu-col-span-6' }
      ],
      arrListDown: {
        FinanceBusinessSegments: [],
        FinanceProductType: [],
        FinanceProduct: [],
        FinanceTaxRate: [],
        FinanceTaxName: []
      },
      rules: {}
    }
  },

  mounted () {
    this.getEnum()
    this.query()
  },

  methods: {
    // 监听所有调用的方法
    listenCall (methodsWords, ...values) {
      this[methodsWords](...values)
    },

    // 数据字典
    getEnum () {
      Common.getSelectIdList('FinanceBusinessSegments', this.arrListDown)
      Common.getSelectIdList('FinanceProductType', this.arrListDown)
      Common.getSelectIdList('FinanceProduct', this.arrListDown)
      Common.getSelectIdList('FinanceTaxRate', this.arrListDown)
      Common.getSelectIdList('FinanceTaxName', this.arrListDown)
    },
 
    // 搜索
    searchFn () {
      this.tableComone.pageIndex = 1
      this.query()
    },

    /**
     * 分页页码变动
     * @param {Number} val pageIndex
     */
    onChangePage (val) {
      this.tableComone.pageIndex = val
      this.query()
    },
    /**
     * 分页页码变动
     * @param {Number} val pageSize
     */
    onpageSizeChange (val) {
      this.tableComone.pageIndex = 1
      this.tableComone.pageSize = val
      this.query()
    },
    /**
     * 分页查询接口
     * @param {Object} obj {pageIndex, pageSize}
     */
    async query () {
      var params = {
        HasPaging: true,
        PageIndex: this.tableComone.pageIndex,
        PageSize: this.tableComone.pageSize
      }
      let res = await FinanceProductApi.getFinanceProductList({...params, ...this.formData})
      console.log('获取列表res: ', res)
      if (res.status === 200 && res.data.success) {
        this.tableData = res.data.data
        this.tableComone.totalRows = res.data.totalRows
      } else {
        this.tableData = []
      }
    },

    // 表格选择操作
    handleOnSelectChange (value) {
      this.selectedArr = value.map((item) => {
        return item
      })
      this.selectedId = value.map((item) => {
        return item.id
      })
      console.log('列表勾选数组1', this.selectedArr)
      console.log('列表勾选数组2', this.selectedId)
    },

    // 显示新建/编辑弹窗
    handleShowCreateModal (id) {
      this.$refs.CreateModal.isCreate = id ? false : true
      this.$refs.CreateModal.arrListDown = this.arrListDown
      console.log('this.$refs.CreateModal.arrListDown: ', this.$refs.CreateModal.arrListDown)
      this.$refs.CreateModal.show()
      if (id) {
        this.$refs.CreateModal.getModel(id)
      }
    },

    // 是否撤回前询问
    isDeleteModal (id) {
      this.$Modal.confirm({
        title: '确认是否删除！',
        onOk: () => {
          this.handleDelete(id)
        }
      })
    },

    async handleDelete (id) {
      FinanceProductApi.delete({id: id}).then(resp => {
        if (resp.data.success) {
          this.$Message.success(resp.data.message)
          this.query()
        } else {
          this.$Message.error(resp.data.message)
        }
      })
    },

    // 导出发票申请
    // async handleExport () {
    //   FinanceProductApi.export(this.formData).then(resp => {
    //     console.log('导出发票申请resp: ', resp)
    //     if (resp.status == 200 && resp.data.success) {
    //       console.log('返回链接: ', resp.data.message)
    //       window.open(resp.data.message, '_blank')
    //     } else {
    //       this.$Message.error(resp.data.message)
    //     }
    //   })
    // },

    // 日期选择
    // handleChangeDatePicker (val, model) {
    //   this.formData[model] = val
    // },

    // 启用/禁用
    async enabledList (id, status) {
      var params = {
        id: id,
        status: status
      }
      let res = await FinanceProductApi.enableFinanceProduct(params)
      if (res.status === 200 && res.data.success) {
        this.$Message.success(res.data.message)
        this.query()
      } else {
        this.$Message.error({
          content: res.data.message,
          duration: 10,
          closable: true
        })
      }
      this.selectedId = []
      this.selectedIdArr = []
    },
  } // methods end
}
</script>

<style lang="less" scoped>

</style>
